package com.linkhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linkhouse.domain.bo.UserBO;
import com.linkhouse.domain.vo.UserVO;
import com.linkhouse.mapper.UserMapper;
import com.linkhouse.service.UserService;
import com.linkhouse.utils.ShiroUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author Sheepherder
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2022-05-24 10:45:34
*/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Resource
    private UserMapper userMapper;


    @Override
    public int getCode(Long phone) {
        try {
            rocketMQTemplate.convertAndSend("PhoneCode_Topic",phone);
            return 1;
        }catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public UserVO selectPhone(Long phone) {
        return userMapper.selectOne(new QueryWrapper<UserVO>()
                .eq("phone",phone)
        );
    }

    @Override
    public int registerUser(UserVO user) {
        return userMapper.insert(user);
    }


    @Override
    public int updateUserById(UserBO userBO, Long phone) {

        String salt = ShiroUtils.generateSalt(6);
        String password = ShiroUtils.encryptPassword("MD5", userBO.getPassword(), salt, 3);

        UserVO user = new UserVO();
        BeanUtils.copyProperties(userBO,user);

        System.out.println("1111");
        System.out.println("1111");
        System.out.println(user.getPhone());
        System.out.println("2222");
        System.out.println("2222");

        user.setSalt(salt);
        user.setPassword(password);
        return userMapper.update(user,new QueryWrapper<UserVO>()
                .eq("phone",user.getPhone())
        );
    }
}




